package com.study.constrollers;

import com.study.common.Message;
import com.study.pojo.Student;
import com.study.services.StudentService;
import com.study.util.IDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 * Created by bunny on 2017/8/31.
 */

@Controller
@RequestMapping("/student")
public class StudentController {
    private Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String studentdo(
        HttpServletRequest request,
        HttpServletResponse response) {
        return "student/student";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Message doRegerstion(
            HttpServletRequest request,
            HttpServletResponse response) {
        Message msg = new Message();

        try {
            List<Student> result = studentService.findAll();

            msg.setCode(true);
            msg.setData(result);
        } catch (Exception ex) {
            logger.error(ex.getStackTrace().toString());
            ex.printStackTrace();

            msg.setCode(false);
        }

        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Message delete(
        HttpServletResponse response,
        HttpServletRequest request,
        String id) {
        Message msg = new Message();

        logger.info(id);

        try {
            int result = studentService.delete(id);

            if (result == 1) {
                msg.setCode(true);
            } else {
                msg.setCode(false);
            }
        } catch (Exception ex) {
            logger.error(ex.getStackTrace().toString());
            ex.printStackTrace();

            msg.setCode(false);
        }

        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message add(
        HttpServletRequest request,
        HttpServletResponse response,
        Student student){
        Message msg = new Message();

        try {
            student.setSid(IDGenerator.getInstance().nextId());

            int result = studentService.add(student);

            if (result == 1) {
                msg.setCode(true);
                msg.setData(student.getSid());
            } else {
                msg.setCode(false);
            }
        } catch (Exception ex) {
            logger.error(ex.getStackTrace().toString());
            ex.printStackTrace();

            msg.setCode(false);
        }

        return msg;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Message updata(HttpServletRequest request,HttpServletResponse response,Student student){
        Message msg = new Message();

        try{
            int result = studentService.update(student);

            if(result == 1){
                msg.setCode(true);
            } else {
                msg.setCode(false);
            }
        }catch (Exception ex){
            logger.error(ex.getStackTrace().toString());
            ex.printStackTrace();

            msg.setCode(false);
        }
        return msg;
    }

}
